package cn.lsh.kafka.db.service.impl;

import cn.lsh.kafka.db.dao.OffsetMapper;
import cn.lsh.kafka.db.entity.OffsetEntity;
import cn.lsh.kafka.db.service.BaseService;
import cn.lsh.kafka.db.service.OffsetService;
import cn.lsh.kafka.db.service.ServiceFactory;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.util.Date;
import java.util.List;

/**
 * kafka偏移量(Offset)表服务实现类
 *
 * @author makejava
 * @since 2020-12-08 14:33:14
 */
public class OffsetServiceImpl extends BaseService<OffsetMapper> implements OffsetService {

    // private OffsetMapper offsetMapper;

    public OffsetServiceImpl(SqlSession sqlSession) {
        super(sqlSession);
    }

    /*public OffsetServiceImpl() {
        TransactionFactory factory = new JdbcTransactionFactory();
        DataSource dataSource = new PooledDataSource("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/bigdata", "root", "admin");
        Environment environment = new Environment("ID", factory, dataSource);
        Configuration conf = new Configuration(environment);
        conf.addMapper(OffsetMapper.class);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(conf);
        this.sqlSession = sessionFactory.openSession();
        offsetMapper = sqlSession.getMapper(OffsetMapper.class);
    }*/

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OffsetEntity queryById(Integer id) {
        return this.mapper.queryById(id);
    }

    @Override
    public OffsetEntity queryByCond(String groupId, String topic, int partitionId) {
        return this.mapper.queryByCond(groupId, topic, partitionId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<OffsetEntity> queryAllByLimit(int offset, int limit) {
        return this.mapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param offset 实例对象
     * @return 实例对象
     */
    @Override
    public OffsetEntity insert(OffsetEntity offset) {
        this.mapper.insert(offset);
        return offset;
    }

    /**
     * 修改数据
     *
     * @param offset 实例对象
     * @return 实例对象
     */
    @Override
    public OffsetEntity update(OffsetEntity offset) {
        this.mapper.update(offset);
        return this.queryById(offset.getId());
    }

    @Override
    public void update(int id, long value) {
        OffsetEntity offset = new OffsetEntity();
        offset.setId(id);
        offset.setValue(value);
        offset.setUpdateDate(new Date());
        this.mapper.update(offset);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        boolean flag = this.mapper.deleteById(id) > 0;
        return flag;
    }

    public static void main(String[] args) {
        // OffsetService service = new OffsetServiceImpl();
        OffsetService service = ServiceFactory.buildService(OffsetService.class);
        OffsetEntity entity = new OffsetEntity();
        entity.setGroupId("11111");
        entity.setPartitionId(1);
        entity.setTopic("adad");
        entity.setValue(10L);
        Date now = new Date();
        entity.setCreateDate(now);
        entity.setUpdateDate(now);
        service.insert(entity);
        OffsetEntity offsetEntity = service.queryById(5);
        System.out.println(offsetEntity);
        ServiceFactory.commitAndClose();
    }
}
